package com.sntk.organauthomanager.application.controller;

import com.alibaba.fastjson.JSON;
import com.sntk.organauthomanager.application.dto.*;
import com.sntk.organauthomanager.application.service.RoleApplicationService;
import com.sntk.organauthomanager.infrastructure.session.LoginSessionInfo;
import com.sntk.organauthomanager.infrastructure.validation.Insert;
import com.sntk.organauthomanager.infrastructure.validation.Update;
import com.sntk.organauthomanager.infrastructure.validation.Validation;
import com.sntk.organauthomanager.query.RoleQuery;
import com.sntk.organauthomanager.query.param.RoleCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: UserController
 * @author: wgchao
 * @createTime: 2020/7/21 10:08 AM
 * @Description: TODO
 */
@RestController
@RequestMapping("/sinohis/osm/external/sys/role")
@Api(value="RoleController",tags={"角色信息管理"})
public class RoleController {
    @Autowired
    private RoleApplicationService roleApplicationService;
    @Autowired
    private RoleQuery roleQuery;

    @PostMapping("/{institutionNumber}")
    @ApiOperation(value="添加角色")
    public ResponseData addRoleToOrgan(@ApiParam(name="institutionNumber",value="机构编号",required=true)
                                           @PathVariable String institutionNumber,
                                       @ApiParam(name="creator",value="角色信息",required=true)
                                           @RequestBody @Validated(value = {Insert.class}) RoleCreator creator, BindingResult result, HttpSession httpSession){
        List<?> l = Validation.getValidation(result);
        if(l.size() != 0) {
            return ResponseData.error(JSON.toJSONString(l));
        }
        roleApplicationService.addRoleToOrgan(institutionNumber, creator, LoginSessionInfo.generate(httpSession).getOperator());
        return ResponseData.success();
    }

    @PutMapping
    @ApiOperation(value="修改角色信息")
    public ResponseData modifyRoleInfo(@ApiParam(name="roleDTO",value="角色信息",required=true)
                                        @RequestBody @Validated(value = {Update.class}) RoleDTO roleDTO, BindingResult result, HttpSession httpSession){
        List<?> l = Validation.getValidation(result);
        if(l.size() != 0) {
            return ResponseData.error(JSON.toJSONString(l));
        }
        roleApplicationService.modifyRoleInfo(roleDTO, LoginSessionInfo.generate(httpSession).getOperator());
        return ResponseData.success();
    }

    @DeleteMapping("/{institutionNumber}/{roleCode}")
    @ApiOperation(value="删除角色")
    public ResponseData removeRole(@ApiParam(name="institutionNumber",value="机构编号",required=true)
                                   @PathVariable String institutionNumber,
                                   @ApiParam(name="roleCode",value="角色码",required=true)
                                   @PathVariable String roleCode, HttpSession httpSession){
        roleApplicationService.removeRole(institutionNumber, roleCode, LoginSessionInfo.generate(httpSession).getOperator());
        return ResponseData.success();
    }

    @GetMapping("/page/condition")
    @ApiOperation(value="分页条件查询角色信息")
    public ResponseData<PageData<RoleVO>> searchRolesOfCondition(@ApiParam(name="roleCondition",value="分页查询条件",required=true)
                                                                         RoleCondition roleCondition, HttpSession httpSession){
        PageData<RoleVO> voPageData = roleQuery.searchRolesOfCondition(roleCondition, LoginSessionInfo.generate(httpSession).getOperatorInfo());
        return ResponseData.success().setData(voPageData);
    }

    @GetMapping("/{institutionNumber}/{roleCode}")
    @ApiOperation(value="查询角色信息")
    public ResponseData<UserDTO> searchRole(@ApiParam(name="institutionNumber",value="机构编号",required=true)
                                            @PathVariable String institutionNumber,
                                            @ApiParam(name="roleCode",value="角色码",required=true)
                                            @PathVariable String roleCode){
        RoleDTO roleDTO = roleQuery.searchRole(institutionNumber, roleCode);
        return ResponseData.success().setData(roleDTO);
    }

    /**
     * 为角色分配资源权限
     * @return
     */
    @PostMapping("assignResourcesToRole")
    @ApiOperation(value="为角色分配资源权限")
    public ResponseData assignResourcesToRole(@ApiParam(name="roleDTO",value="角色信息",required=true)
                                              @RequestBody @Validated(value = {Insert.class}) ResourcesRole resourcesRole,
                                              BindingResult result,
                                              HttpSession httpSession){
        List<?> l = Validation.getValidation(result);
        if(l.size() != 0) {
            return ResponseData.error(JSON.toJSONString(l));
        }
        roleApplicationService.assignResourcesToRole(resourcesRole.getInstitutionNumber(),resourcesRole.getRoleCode(),resourcesRole.getMarks(),LoginSessionInfo.generate(httpSession).getOperator());
        return ResponseData.success();
    }

    /**
     * 通过角色查询人员信息
     * @param roleCondition
     * @param httpSession
     * @return
     */
    @GetMapping("searchUserRole")
    @ApiOperation(value="通过角色查询人员信息")
    public ResponseData<PageData<UserRoleDTO>> searchUserRole(@ApiParam(name="roleCondition",value="分页查询条件",required=true)
                                                                      RoleCondition roleCondition,
                                                              HttpSession httpSession){
        PageData<UserRoleDTO> userRolesDTO = roleQuery.searchUsersByRole(roleCondition,LoginSessionInfo.generate(httpSession).getOperatorInfo());
        return ResponseData.success().setData(userRolesDTO);
    }


    //通过人员查询角色信息
    @GetMapping("/searchUserRoleByUname/{uname}")
    @ApiOperation(value="通过人员查询角色信息")
    public ResponseData searchUserRoleByUname(@ApiParam(name="uname",value="用户编号",required=true)
                                              @PathVariable String uname,
                                              HttpSession httpSession){
        List<RoleVO> roleVOList = roleQuery.searchUserRoleByUname(uname,LoginSessionInfo.generate(httpSession).getOperatorInfo());
        return ResponseData.success().setData(roleVOList);
    }

    /**
     * 通过机构编号查询机构下的角色信息
     * @param institutionNumber
     * @param httpSession
     * @return
     */
    @GetMapping("/searchRolesByinstitutionNumber/{institutionNumber}")
    @ApiOperation(value="通过机构编号查询机构下的角色信息")
    public ResponseData<List<RoleVO>> searchRolesByinstitutionNumber(@ApiParam(name="institutionNumber",value="机构编号",required=true)
                                                       @PathVariable String institutionNumber,
                                                                     HttpSession httpSession){
        List<RoleVO> roleVOList = roleQuery.searchRolesByinstitutionNumber(institutionNumber,LoginSessionInfo.generate(httpSession).getOperatorInfo());
        return  ResponseData.success().setData(roleVOList);
    }

}
